﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_CmdPlacementSelect : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IPositionTypeBLL ptBLL = new PositionTypeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IClassBLL cBLL = new ClassBLL();
        private IJobSiteBLL jsBLL = new JobSiteBLL();
        private IMovmentBLL movBLL = new MovmentBLL();
        private ICommandDetailBLL dtlsBLL = new CommandDetailBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private SalaryGroupBLL salBLL = new SalaryGroupBLL();
        private RecruitCatalogBLL catBLL = new RecruitCatalogBLL();
        private RecruitApplicantBLL appBLL = new RecruitApplicantBLL();
        private FieldInfo fieldInfo = new FieldInfo();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            txtSalary.Attributes.Add("onkeypress", "return isNumberKey(event)");

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                bindMovmentCombo();
                bindJobSiteCombo();                

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {                    
                    lblCmdAddSeq.Text = DataBinder.Eval(DataItem, "cmd_dtls_seq").ToString();

                    //// ชื่อ  สกุล
                    cmbTmpEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FULL_NAME").ToString(), DataBinder.Eval(DataItem, "TMP_EMP_SEQ").ToString()));
                    cmbTmpEmployee.SelectedValue = DataBinder.Eval(DataItem, "TMP_EMP_SEQ").ToString();                    

                    // การศึกษา
                    cmbTmpEducation.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_QUAL_NAME").ToString(), DataBinder.Eval(DataItem, "TMP_EDU_SEQ").ToString()));
                    cmbTmpEducation.SelectedValue = DataBinder.Eval(DataItem, "TMP_EDU_SEQ").ToString();

                    movmentCombo.SelectedValue = DataBinder.Eval(DataItem, "MOVE_SEQ").ToString();
                    
                    //ตำแหน่งและส่วนราชการที่บรรจุ
                    cmbNewPosition.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "NEW_POS_TEXT").ToString(), DataBinder.Eval(DataItem, "NEW_POS_SEQ").ToString()));
                    cmbNewPosition.SelectedValue = DataBinder.Eval(DataItem, "NEW_POS_SEQ").ToString();
                    cmbNewClass.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "NEW_CLASS_NAME").ToString(), DataBinder.Eval(DataItem, "NEW_CLASS_SEQ").ToString()));
                    cmbNewClass.SelectedValue = DataBinder.Eval(DataItem, "NEW_CLASS_SEQ").ToString();

                    jobSiteCombo.SelectedValue = DataBinder.Eval(DataItem, "NEW_JOB_SITE_SEQ").ToString();
                    txtSalary.Text = DataBinder.Eval(DataItem, "NEW_SALARY", "{0:N2}").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "START_DATE", "{0:dd/MM/yyyy}").ToString());
                }
            }
        }

        private void bindJobSiteCombo()
        {
            DataSet ds = jsBLL.LoadJobSites("", SystemInfo._JobsiteExternal, "", "");
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                jobSiteCombo.DataSource = dt;
                jobSiteCombo.DataBind();
            }
        }

        private void bindMovmentCombo()
        {
            IList<MovmentInfo> infoList = movBLL.ListMovments(Session["EMPTYPE"].ToString());

            movmentCombo.DataSource = infoList;
            movmentCombo.DataBind();
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            string strPosition = ((RadComboBox)cmbNewPosition).Text;
            string[] str = strPosition.Split('-');

            dtlsBLL.Insert(int.Parse(Session["CMD"].ToString())
                , (int?)null
                , string.IsNullOrEmpty(txtStartDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtStartDate.GetStringValue())
                , string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue)
                , str[0]
                , str[1]//cmbNewPosition.SelectedItem.Text// txtPosition.Text.Trim()
                , string.IsNullOrEmpty(cmbNewClass.SelectedValue) ? (int?)null : int.Parse(cmbNewClass.SelectedValue)
                , string.IsNullOrEmpty(jobSiteCombo.SelectedValue) ? (int?)null : int.Parse(jobSiteCombo.SelectedValue)
                , jobSiteCombo.SelectedItem.Text
                , string.IsNullOrEmpty(txtSalary.Text) ? (double?)null : double.Parse(txtSalary.Text)
                , (double?)null
                , (int?)null
                , "", "", (int?)null, (int?)null, ""
                , (int?)null
                , ""
                , (int?)null //string.IsNullOrEmpty(entPostTypeCombo.SelectedValue) ? (int?)null : Int32.Parse(entPostTypeCombo.SelectedValue)
                , (int?)null //string.IsNullOrEmpty(entClassCombo.SelectedValue) ? (int?)null : Int32.Parse(entClassCombo.SelectedValue)
                , "" //txtEntJobSiteName.Text
                , (DateTime?)null //string.IsNullOrEmpty(txtEntDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtEntDate.GetStringValue())
                , string.IsNullOrEmpty(cmbTmpEmployee.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEmployee.SelectedValue)
                , string.IsNullOrEmpty(cmbTmpEducation.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEducation.SelectedValue)
                , (int?)null
                , string.IsNullOrEmpty(movmentCombo.SelectedValue) ? (int?)null : Int32.Parse(movmentCombo.SelectedValue)
                , (DateTime?)null
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strPosition = ((RadComboBox)cmbNewPosition).Text;
            string[] str = strPosition.Split('-');

            dtlsBLL.Update(int.Parse(lblCmdAddSeq.Text)
                , (int?)null
                , string.IsNullOrEmpty(txtStartDate.GetStringValue()) ? (DateTime?)null : DateTime.Parse(txtStartDate.GetStringValue())
                , string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue)
                , str[0]
                , str[1]
                , string.IsNullOrEmpty(cmbNewClass.SelectedValue) ? (int?)null : int.Parse(cmbNewClass.SelectedValue)
                , string.IsNullOrEmpty(jobSiteCombo.SelectedValue) ? (int?)null : int.Parse(jobSiteCombo.SelectedValue)
                , jobSiteCombo.SelectedItem.Text
                , string.IsNullOrEmpty(txtSalary.Text) ? (double?)null : double.Parse(txtSalary.Text)
                , (double?)null
                , (int?)null
                , "", "", (int?)null, (int?)null, ""
                , (int?)null
                , ""
                , (int?)null
                , (int?)null
                , ""
                , (DateTime?)null
                , string.IsNullOrEmpty(cmbTmpEmployee.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEmployee.SelectedValue)
                , string.IsNullOrEmpty(cmbTmpEducation.SelectedValue) ? (int?)null : Int32.Parse(cmbTmpEducation.SelectedValue)
                , (int?)null
                , string.IsNullOrEmpty(movmentCombo.SelectedValue) ? (int?)null : Int32.Parse(movmentCombo.SelectedValue)
                , (DateTime?)null
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbNewPosition_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
        {
            cmbNewPosition.Items.Clear();

            DataSet ds = posBLL.LoadPositions((int?)null, 1, (int?)null, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbNewPosition.Items.Add(new RadComboBoxItem(dt.Rows[i]["POS_TEXT"].ToString(), dt.Rows[i]["POS_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["POS_REF"].ToString();
                tdPosRef.Width = Unit.Pixel(100);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["CLASS_SPEC_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbNewPosition.Items[j].FindControl("tableJobsite")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbNewPosition_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Nullable<int> Sequence = string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue);

            DataSet ds = posBLL.LoadPositions(Sequence, (int?)null, (int?)null, "");
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    jobSiteCombo.SelectedValue = dt.Rows[0]["job_site_seq"].ToString();
                }
            }
        }

        protected void cmbTmpEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbTmpEmployee.Items.Clear();

            DataSet ds = catBLL.LoadCatalogDtls((int?)null, (int?)null, 100);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbTmpEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["TMP_EMP_TEXT"].ToString(), dt.Rows[i]["TMP_EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["CAT_NO"].ToString();
                tdPosRef.Width = Unit.Pixel(50);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["TMP_EMP_TEXT"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = string.Format("{0:dd/MM/yyyy}", dt.Rows[i]["BIRTH_DATE"]);
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["CITIZEN"].ToString();
                tdJobName.Width = Unit.Pixel(50);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbTmpEmployee.Items[j].FindControl("tableTmpEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbTmpEducation_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbTmpEducation.Items.Clear();

            string strTmpSeq = lblTmpEmpSeq.Text;
            Nullable<int> TmpEmployee = string.IsNullOrEmpty(strTmpSeq) ? (int?)null : Int32.Parse(strTmpSeq);

            DataSet ds = appBLL.LoadEducations((int?)null, TmpEmployee);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbTmpEducation.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_QUAL_NAME"].ToString(), dt.Rows[i]["TMP_EDU_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["EDU_DEGREE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);
                
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["EDU_QUAL_NAME"].ToString();
                tdPosRef.Width = Unit.Pixel(150);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["EDU_MAJOR_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["EDU_INS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(150);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                ((Table)cmbTmpEducation.Items[j].FindControl("tableTmpEducation")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbNewClass_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbNewClass.Items.Clear();

            Nullable<int> PosSequence = string.IsNullOrEmpty(cmbNewPosition.SelectedValue) ? (int?)null : Int32.Parse(cmbNewPosition.SelectedValue);

            DataSet ds = null;
            DataTable dt = null;

            ds = cBLL.LoadClassList((int?)null, 1, (int?)null, (int?)null, (int?)null);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbNewClass.Items.Add(new RadComboBoxItem(dt.Rows[i]["CLASS_NAME"].ToString(), dt.Rows[i]["CLASS_SEQ"].ToString()));
                TableRow tr = new TableRow();

                TableCell tdClassName = new TableCell();
                Label lblClassName = new Label();
                lblClassName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdClassName.Width = Unit.Pixel(150);
                tdClassName.Controls.Add(lblClassName);
                tr.Cells.Add(tdClassName);

                TableCell tdSalMin = new TableCell();
                Label lblSalMin = new Label();
                lblSalMin.Text = dt.Rows[i]["SAL_MIN"].ToString();
                tdSalMin.Width = Unit.Pixel(50);
                tdSalMin.Controls.Add(lblSalMin);
                tr.Cells.Add(tdSalMin);

                TableCell tdSalMax = new TableCell();
                Label lblSalMax = new Label();
                lblSalMax.Text = dt.Rows[i]["SAL_MAX"].ToString();
                tdSalMax.Width = Unit.Pixel(50);
                tdSalMax.Controls.Add(lblSalMax);
                tr.Cells.Add(tdSalMax);

                ((Table)cmbNewClass.Items[j].FindControl("tableNewClass")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

    }
}